#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
AI Class Elysium Python 服务
监听端口: 18080
提供健康检查接口和文件上传到TOS的接口
"""

import sys
from flask import Flask

# 检查 Python 版本
if sys.version_info < (3, 11):
    print("错误: 此服务需要 Python 3.11 或更高版本")
    sys.exit(1)

# 导入模块
from core.extensions import init_extensions, init_services
from routes.main_routes import main_bp
from routes.amap_routes import amap_bp
from routes.text_to_image_routes import text_to_image_bp
from routes.command_routes import command_bp
from socket_events.connection_events import register_connection_events
from socket_events.asr_events import register_asr_events
from socket_events.tts_events import register_tts_events


def create_app():
    """
    创建并配置Flask应用
    
    Returns:
        app: Flask应用实例
        socketio: SocketIO实例
    """
    # 创建 Flask 应用
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'voice_control_secret_key_2024'
    
    # 初始化扩展
    socketio = init_extensions(app)
    
    # 初始化服务
    init_services()
    
    # 注册蓝图（HTTP路由）
    app.register_blueprint(main_bp)
    app.register_blueprint(amap_bp)
    app.register_blueprint(text_to_image_bp)
    app.register_blueprint(command_bp)
    
    # 注册WebSocket事件
    register_connection_events(socketio)
    register_asr_events(socketio)
    register_tts_events(socketio)
    
    return app, socketio


if __name__ == '__main__':
    print("启动 语音控制 服务...")
    print(f"Python 版本: {sys.version}")
    print("监听端口: 18080")
    print("\n可用的HTTP接口:")
    print("  - 健康检查接口: http://localhost:18080/check")
    print("  - 系统状态接口: http://localhost:18080/status")
    print("  - 根路径接口: http://localhost:18080/")
    print("\n高德地图接口:")
    print("  - 服务状态: http://localhost:18080/amap/status")
    print("  - 工具列表: http://localhost:18080/amap/tools")
    print("  - POI搜索: http://localhost:18080/amap/search")
    print("  - 天气查询: http://localhost:18080/amap/weather")
    print("  - 路线规划: http://localhost:18080/amap/route")
    print("  - 地理编码: http://localhost:18080/amap/geocode")
    print("  - 逆地理编码: http://localhost:18080/amap/regeo")
    print("  - 工具调用: http://localhost:18080/amap/call_tool")
    print("\n文生图接口:")
    print("  - 图片生成: http://localhost:18080/text_to_image/generate")
    print("  - 服务状态: http://localhost:18080/text_to_image/status")
    print("\nWebSocket接口:")
    print("  - 连接地址: ws://localhost:18080/socket.io/")
    print("  - 支持事件: connect, disconnect, ping, get_status")
    print("  - ASR事件: start_streaming_asr, streaming_audio_chunk, stop_streaming_asr")
    print("  - TTS事件: start_streaming_tts, stop_streaming_tts")
    print("\n")
    
    # 创建应用
    app, socketio = create_app()
    
    # 启动服务，监听 18080 端口
    socketio.run(
        app,
        host='0.0.0.0',  # 监听所有 IP
        port=18080,      # 指定端口
        debug=True,      # 开发模式
        allow_unsafe_werkzeug=True  # 允许在开发模式下使用
    )
